package com.asd.system.controller;

import com.asd.common.Result;
import com.asd.system.entity.Role;
import com.asd.system.service.IRoleService;
import com.asd.util.oConvertUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author
 * @createtime 2021-10-29 9:08
 * @description 角色的CRUD
 */
@RestController
@RequestMapping("/api/role")
@Api(tags = "角色Api")
public class RoleController {

    @Autowired
    private IRoleService roleService;



    /**
     * 分页列表查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "角色-分页列表查询", notes = "角色-分页列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        Page<Role> page = new Page<Role>(pageNo, pageSize);


        List<Role> list = roleService.selectUserPage(page);

        Page<Role> pageList = page.setRecords(list);

        return Result.ok(pageList);
    }

    /**
     *   添加
     * @param role
     * @return
     */
    @ApiOperation(value = "角色-添加", notes = "角色-添加")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<Role> add(@RequestBody Role role) {
        Result<Role> result = new Result<Role>();
        try {
           roleService.save(role);
            result.success("添加成功！");
        } catch (Exception e) {
            result.error500("操作失败");
        }
        return result;
    }


    /**
     *   通过id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "角色-删除", notes = "角色-删除")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        roleService.deleteRole(id);
        return Result.ok("删除角色成功");
    }

//    /**
//     *  批量删除
//     * @param ids
//     * @return
//     */
//    @ApiOperation(value = "角色-批量删除", notes = "角色-批量删除")
//    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
//    public Result<Role> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//        Result<Role> result = new Result<Role>();
//        if(oConvertUtils.isEmpty(ids)) {
//            result.error500("未选中角色！");
//        }else {
//            roleService.deleteBatchRole(ids.split(","));
//            result.success("删除角色成功!");
//        }
//        return result;
//    }

    /**
     * 通过id查询
     * @param id
     * @return
     */

    @ApiOperation(value = "角色-通过id查询", notes = "角色-通过id查询")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<Role> queryById(@RequestParam(name="id",required=true) String id) {
        Result<Role> result = new Result<Role>();
        Role Role = roleService.getById(id);
        if(Role==null) {
            result.error500("未找到对应实体");
        }else {
            result.setResult(Role);
            result.setSuccess(true);
        }
        return result;
    }

    @ApiOperation(value = "角色-全部查询", notes = "角色-全部查询")
    @RequestMapping(value = "/queryall", method = RequestMethod.GET)
    public Result<List<Role>> queryall() {
        Result<List<Role>> result = new Result<>();
        List<Role> list = roleService.list();
        if(list==null||list.size()<=0) {
            result.error500("未找到角色信息");
        }else {
            result.setResult(list);
            result.setSuccess(true);
        }
        return result;
    }

}
